package de.reminder.client.widgets;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasMouseDownHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DateBox;

import de.reminder.client.section.SingleReminderSection;
import de.reminder.shared.model.FacebookUser;
import de.reminder.shared.model.Reminder;

public class ReminderView extends Composite{
	private static final int statusActive = 1;
	private static final int statusInactive = 2;
	private static final int readOnly = 3;
	
	private AbsolutePanel boundary;
	private VerticalPanel content;
	private PushButton buttonSave;
	private TextBox textboxTitle;
	private TimePicker timepicker;
	private TextArea textareaDescription;
	private PhotoClip photoclip;
	private int status = 1;
	
	private EditUsers addUserDialog;
		
	private List<FacebookUser> users;
	
	public ReminderView(){
		boundary = new AbsolutePanel();
		content = new VerticalPanel();
		buttonSave = new PushButton(new Image("images/reminder/buttonSave.png"));
		buttonSave.getElement().setClassName("");
		textboxTitle = new TextBox();
		textareaDescription = new TextArea();
		timepicker = new TimePicker();
		photoclip = new PhotoClip(80,this);
		
		addUserDialog = new EditUsers(this);
		
		users = new LinkedList<FacebookUser>();
		
		buttonSave.addClickHandler(new SaveClickHandler(this));
				
		boundary.add(photoclip,320,0);
		boundary.add(content,20,45);
		boundary.add(buttonSave,440,250);
		boundary.setPixelSize(510, 300);
		boundary.getElement().setId("Rahmen");
		
		content.add(textboxTitle);
		content.add(timepicker);
		content.add(textareaDescription);
		
		textboxTitle.getElement().setId("Title");
		textboxTitle.addStyleName("Handwritten");
		
		timepicker.getElement().setId("Timepicker");
		timepicker.getElement().setClassName("Handwritten");
		
		textareaDescription.getElement().setId("Description");
		textareaDescription.addStyleName("Handwritten");
		initWidget(boundary);
		this.getElement().setClassName("Reminder");
	}
	
	public ReminderView(Reminder data){
		this();
		this.setDisplayOnly();
		for(FacebookUser f : data.getUser()){
			this.addUser(f);
		}
		textboxTitle.setText(data.getTitle());
	}
	
	public void setDisplayOnly(){
		status = readOnly;
		textboxTitle.setReadOnly(true);
		textboxTitle.getElement().setClassName("Handwritten");
		textareaDescription.setReadOnly(true);
		timepicker.setReadOnly();
		buttonSave.setEnabled(false);
	}
	
	public void addUser(FacebookUser user){
		users.add(user);
		photoclip.addUser(user);
	}
	
	private void toggleStatus(){
		if(status == statusActive)
			setInactive();
		else
			setActive();
	}
	
	private void setActive(){
		if(status == statusActive)
			return;
		
		this.getElement().setClassName("Reminder");
		
		status = statusActive;
	}
	
	private void setInactive(){
		if(status == statusInactive)
			return;
		
		this.getElement().setClassName("Reminder-Inactive");
		
		status = statusInactive;
	}
	
	
	private class TimePicker extends Composite{
		private HorizontalPanel main;
		private DateBox datePicker;
		
		private TimePicker2 hourPicker;
		private TimePicker2 minutePicker;
		
		public TimePicker(){
			main = new HorizontalPanel();
			datePicker = new DateBox();
			datePicker.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("d'.' MMM yyyy")));
			datePicker.getElement().setClassName("Handwritten");
			datePicker.setValue(new Date());
			datePicker.setWidth("100px");
			main.add(datePicker);
			
			String[] houres = {"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"};
			hourPicker = new TimePicker2(houres);
			main.add(hourPicker);
			
			Label label = new Label(":");
			label.getElement().setClassName("Handwritten");
			label.getElement().getStyle().setPaddingTop(3, Unit.PX);
			main.add(label);
			
			String[] minutes = {"00","05","10","15","20","25","30","35","40","45","50","55"};
			minutePicker = new TimePicker2(minutes);
			main.add(minutePicker);
			
			initWidget(main);
		}
		
		public Date getDate(){
			Date d = datePicker.getValue();
			d.setHours(hourPicker.getValue());
			d.setMinutes(minutePicker.getValue());
			return d;
		}
		
		public void setReadOnly(){
			datePicker.setEnabled(false);
			hourPicker.setReadOnly();
			minutePicker.setReadOnly();
		}
	}
	
	private class SaveClickHandler implements ClickHandler{
		private ReminderView sender;
		public SaveClickHandler(ReminderView v){
			this.sender = v;
		}
		@Override
		public void onClick(ClickEvent event) {
			Reminder r = new Reminder(textboxTitle.getText(), textareaDescription.getText(), timepicker.getDate(), new Date(), users);
			System.out.println("ReminderSaved");
		}
	}

	public void onPhotoClipClicked() {
		if(status == readOnly)
			return;
		addUserDialog.show();
	}
}
